package com.ischoolbar.programmer.dao;

/**
 * @Author lin
 **/

import com.ischoolbar.programmer.model.Page;
import com.ischoolbar.programmer.model.Teacher;
import com.ischoolbar.programmer.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDao extends BaseDao {
    public TeacherDao() {
    }

    public boolean addTeacher(Teacher teacher) {
        String sql = "insert into s_teacher values(null,'" + teacher.getSn() + "','" + teacher.getName() + "'";
        sql = sql + ",'" + teacher.getPassword() + "'," + teacher.getClazzId();
        sql = sql + ",'" + teacher.getSex() + "','" + teacher.getMobile() + "'";
        sql = sql + ",'" + teacher.getQq() + "',null)";
        return this.update(sql);
    }

    public boolean editTeacher(Teacher teacher) {
        String sql = "update s_teacher set name = '" + teacher.getName() + "'";
        sql = sql + ",sex = '" + teacher.getSex() + "'";
        sql = sql + ",mobile = '" + teacher.getMobile() + "'";
        sql = sql + ",qq = '" + teacher.getQq() + "'";
        sql = sql + ",clazz_id = " + teacher.getClazzId();
        sql = sql + " where id = " + teacher.getId();
        return this.update(sql);
    }

    public boolean setTeacherPhoto(Teacher teacher) {
        String sql = "update s_teacher set photo = ? where id = ?";
        Connection connection = this.getConnection();

        try {
            PreparedStatement prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setBinaryStream(1, teacher.getPhoto());
            prepareStatement.setInt(2, teacher.getId());
            return prepareStatement.executeUpdate() > 0;
        } catch (SQLException var5) {
            var5.printStackTrace();
            return this.update(sql);
        }
    }

    public boolean deleteTeacher(String ids) {
        String sql = "delete from s_teacher where id in(" + ids + ")";
        return this.update(sql);
    }

    public Teacher getTeacher(int id) {
        String sql = "select * from s_teacher where id = " + id;
        Teacher teacher = null;
        ResultSet resultSet = this.query(sql);

        try {
            if (resultSet.next()) {
                teacher = new Teacher();
                teacher.setId(resultSet.getInt("id"));
                teacher.setClazzId(resultSet.getInt("clazz_id"));
                teacher.setMobile(resultSet.getString("mobile"));
                teacher.setName(resultSet.getString("name"));
                teacher.setPassword(resultSet.getString("password"));
                teacher.setPhoto(resultSet.getBinaryStream("photo"));
                teacher.setQq(resultSet.getString("qq"));
                teacher.setSex(resultSet.getString("sex"));
                teacher.setSn(resultSet.getString("sn"));
                return teacher;
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
        }

        return teacher;
    }

    public List<Teacher> getTeacherList(Teacher teacher, Page page) {
        List<Teacher> ret = new ArrayList();
        String sql = "select * from s_teacher ";
        if (!StringUtil.isEmpty(teacher.getName())) {
            sql = sql + "and name like '%" + teacher.getName() + "%'";
        }

        if (teacher.getClazzId() != 0) {
            sql = sql + " and clazz_id = " + teacher.getClazzId();
        }

        if (teacher.getId() != 0) {
            sql = sql + " and id = " + teacher.getId();
        }

        sql = sql + " limit " + page.getStart() + "," + page.getPageSize();
        ResultSet resultSet = this.query(sql.replaceFirst("and", "where"));

        try {
            while(resultSet.next()) {
                Teacher t = new Teacher();
                t.setId(resultSet.getInt("id"));
                t.setClazzId(resultSet.getInt("clazz_id"));
                t.setMobile(resultSet.getString("mobile"));
                t.setName(resultSet.getString("name"));
                t.setPassword(resultSet.getString("password"));
                t.setPhoto(resultSet.getBinaryStream("photo"));
                t.setQq(resultSet.getString("qq"));
                t.setSex(resultSet.getString("sex"));
                t.setSn(resultSet.getString("sn"));
                ret.add(t);
            }
        } catch (SQLException var7) {
            var7.printStackTrace();
        }

        return ret;
    }

    public int getTeacherListTotal(Teacher teacher) {
        int total = 0;
        String sql = "select count(*) as total from s_teacher ";
        if (!StringUtil.isEmpty(teacher.getName())) {
            sql = sql + "and name like '%" + teacher.getName() + "%'";
        }

        if (teacher.getClazzId() != 0) {
            sql = sql + " and clazz_id = " + teacher.getClazzId();
        }

        if (teacher.getId() != 0) {
            sql = sql + " and id = " + teacher.getId();
        }

        ResultSet resultSet = this.query(sql.replaceFirst("and", "where"));

        try {
            while(resultSet.next()) {
                total = resultSet.getInt("total");
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
        }

        return total;
    }

    public Teacher login(String name, String password) {
        String sql = "select * from s_teacher where name = '" + name + "' and password = '" + password + "'";
        ResultSet resultSet = this.query(sql);

        try {
            if (resultSet.next()) {
                Teacher teacher = new Teacher();
                teacher.setId(resultSet.getInt("id"));
                teacher.setName(resultSet.getString("name"));
                teacher.setPassword(resultSet.getString("password"));
                teacher.setClazzId(resultSet.getInt("clazz_id"));
                teacher.setMobile(resultSet.getString("mobile"));
                teacher.setPhoto(resultSet.getBinaryStream("photo"));
                teacher.setQq(resultSet.getString("qq"));
                teacher.setSex(resultSet.getString("sex"));
                teacher.setSn(resultSet.getString("sn"));
                return teacher;
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
        }

        return null;
    }

    public boolean editPassword(Teacher teacher, String newPassword) {
        String sql = "update s_teacher set password = '" + newPassword + "' where id = " + teacher.getId();
        return this.update(sql);
    }
}
